<?php

namespace app\admin\controller;

class Picture extends Admin{


    public function index(){
        $Picture = D('Picture');
        $Album = M('album');
        $album_info = $Album->where(['id'=>I('get.aid/d')])->find();
        if(!$album_info){
            return $this->myError('该相册不存在');
        }
        $map['album_id'] = I('get.aid/d');
        $count = $Picture->where($map)->count();
        import('@.util.Page');
        $Page = new \Page($count,18);
        $list = $Picture->where($map)->limit($Page->limit)->order('sort desc,id desc')->select();
        return $this->fetch('index',[
            'list'=>$list,
            'page' => $Page -> pageShow(),
            'album' => $album_info
        ]);
    }

    public function update(){
        $Picture = M('picture');
        if(IS_POST){
            //print_r($_POST);
            if(!preg_match("/^.{1,50}$/",I('post.pname/s'))){
                return '图片名称不能为空';
            }
            if(I('post.sort/d') < 0){
                return '显示排序请输入一个不为负数的整数';
            }
            $data = [
                'pname' => I('post.pname/s'),
                'sort' => I('post.sort/d'),
                'update_time' => I('post.update_time/d')
            ];
            return $Picture->where(['id'=>I('post.pid/d')])->setField($data);
        }else{
            $pid = I('get.pid/d');
            $res = $Picture->where(['id'=>$pid])->find();
            if(!$res){
                return $this->myError('该图片不存在');
            }
            return $this->fetch('update',['res'=>$res]);
        }

    }

    /**
    *   删除
    */
    public function delete(){
        if(IS_POST){
            if(!I('post.pid/d')){
                return '参数错误！';
            }
            $Picture = M('picture');
            $res = $Picture->where(['id'=>I('post.pid/d')])->find();
            $Qiniu = A('qiniu','event');
            $Qiniu->delete(substr(strrchr($res['path'],'/'),1));
            return $Picture->where(['id'=>$res['id']])->delete();
        }
    }

    public function create(){
        $Album = M('album');
        if(!I('get.aid/d')){
            return '参数错误';
        }
        if(!$Album->where(['id'=>I('get.aid')])->field('id')->find()){
            return '参数错误';
        }
        return $this->fetch('create',[
            'aid' => I('get.aid/d')
        ]);
    }

    public function upload(){
        if(!I('post.key/d')){
            return '参数错误！';
        }
        //文件上传
        if($_FILES['file']['size'] < 1){
            return '没有选择文件';
        }
        $Qiniu = A('qiniu','event');
        $res = $Qiniu->upload($_FILES['file']['tmp_name'],'picture_'.I('post.key/d').'_');
        if(!$res){
            return '上传失败！';
        }
        //添加到相册
        $Album = M('album');
        $Album->where(['id'=>I('post.key/d')])->setInc('number',1);
        $Picture = M('picture');
        $data = [
            'pname' => I('post.fileName/s'),
            'path' => $res['key'],
            'hash' => $res['hash'],
            'album_id' => I('post.key/d'),
            'update_time' => I('post.lastModifiedDate/d'),
        ];
        $r = $Picture->add($data);
        $data['pid'] = $r;
        return json_encode($data);
    }

    public function setSort(){

    }
}
